$(function () {
    vm.getCategorys();
    $("#jqGrid").Grid({
        url: '../userdynamic/list',
        height:675,
        colModel: [
			{label: 'ID', name: 'id', index: 'id', key: true,width:15 ,align:'center',hidden: true},
            {label: '编号', name: 'id', index: 'id', key: true,width:15 ,align:'center'},
			{label: '用户编号', name: 'authorid', index: 'authorid', width: 20,align:'center'},
            {label: '内容', name: 'newdes', index: 'newdes', width:20 ,align:'center'},
            {label: '封面', name: 'imageUrl', index: 'imageUrl', width: 30,align:'center',
                formatter:function (value) {
                    if(value!=""){
                        return transImg(value);
                    }else{
                        return "无封面";
                    }
                }
            },
			{label: '点赞', name: 'praise', index: 'praise', width: 15,align:'center'},
            {label: '类型', name: 'typeName', index: 'typeName', width: 15,align:'center'},
            {label: '状态', name: 'enabled', index: 'enabled', width: 20,align:'center',
                formatter: function (value) {
                    if(value == 1)
                        return "<span class=\"label label-success\">上架</span>";
                    if(value == 0)
                        return "<span class=\"label label-danger\">下架</span>";
                }
            },
            {label: '动态类型', name: 'type', index: 'type', width: 20,align:'center',
                formatter: function (value) {
                    if(value == 1)
                        return "<span class=\"label label-success\">图文动态</span>";
                    if(value == 2)
                        return "<span class=\"label label-success\">视频动态</span>";
                }
            },
            {label: '是否审核', name: 'ischeck', index: 'ischeck', width: 20,align:'center',formatter: function (value) {
                    if(value==1){
                        return "<span class=\"label label-success\">已审核</span>";
                    }else{
                        return "<span class=\"label label-warning\">未审核</span>";
                    }
                }
            },
            {label: '推荐位', name: 'isrecommend', index: 'isrecommend', width: 20,align:'center',formatter: function (value) {
                    if(value==1){
                        return "<span class=\"label label-success\">推荐</span>";
                    }else if(value==0){
                        return "<span class=\"label label-danger\">不推荐</span>";
                    }
                }
            },
			{label: '创建日期', name: 'createdat', index: 'createdAt', width: 40,align:'center',formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            }
        ]

    });
    $('#dynamicPicture').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '500px', //高度
        minHeight: '200px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    })
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		cmsNews: {video:''},
		ruleValidate: {
		},
		q: {
            authorid:'',
            type:'',
            begin:'',
            end:'',
            enabled:'',
            dtype:''
		},
        categorys: []//属性类别
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.cmsNews = {video:''};
            vm.categorys = [];
            $('#newdes').editable('setHTML', '');
            vm.getCategorys();
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
            vm.getCategorys();
		},
        saveOrUpdate: function (event) {
            let url = vm.cmsNews.id == null ? "../userdynamic/save" : "../userdynamic/update";
            vm.cmsNews.newdes = $('#newdes').editable('getHTML');
            vm.cmsNews.authorname = $('#authorselect option:selected').text();
            if (vm.cmsNews.enabled ==='0'){
                vm.cmsNews.categoryIDlist=[21]
            }
            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.cmsNews),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../userdynamic/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../userdynamic/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.cmsNews = r.cmsNews;
                    $('#dynamicPicture').editable('setHTML', vm.cmsNews.dynamicPicture);
                    // console.log(vm.cmsNews.video)
                    // this.$refs.video.src = vm.cmsNews.video;

                }
            });


		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page')
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'type':vm.q.type,'authorid':vm.q.authorid,'begin':vm.q.begin,'end':vm.q.end,'enabled':vm.q.enabled,'dtype':vm.q.dtype},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                authorid:'',
                type:'',
                begin:'',
                end:'',
                enabled:'',
                dtype:''
            }
            vm.reload();
        },
        goodsreloadSearch: function() {
            vm.q = {
                authorid:'',
                type:'',
                begin:'',
                end:'',
                enabled:'',
                dtype:''
            }
            vm.reload();
        },
        getCategorys: function () {
			Ajax.request({
				url: "../dynamiccategory/queryAll",
				async: true,
				successCallback: function (r) {
					vm.categorys = r.list;
				}
			});
		},
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        openSpe: function (id) {

            openWindow({
                type: 2,
                title: '选择商品',
                content: '../shop/cmsnewgoods.html?type='+0+'&newsid=' + id
            })
        },
        handleSuccessPicUrl: function (res, file) {
            vm.cmsNews.imageUrl = file.response.url;
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImagePicUrl: function () {
            var url = vm.cmsNews.imageUrl;
            eyeImage(url);
        },

        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        },
        check: function () {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null){
                return;
            }
            Ajax.request({
                url: "../userdynamic/check",
                params: JSON.stringify(ids),
                type: "POST",
                contentType: "application/json",
                async: true,
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        }
	}

});
